package com.jamaskii.dictation;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class ItemAdapter extends ArrayAdapter {
    public ItemAdapter(Context context, int resource, List<ItemData> objects){
        super(context,resource,objects);
    }
    public ArrayList<CheckBox> checkBoxes=new ArrayList<>();
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ItemData itemData=(ItemData)getItem(position);
        switch(itemData.type){
            case ItemType.SHOW_BOOK:
                View showBookView = LayoutInflater.from(getContext()).inflate(R.layout.item_book_show, null);

                TextView labelBookName=showBookView.findViewById(R.id.label_book_name_item_home);
                TextView labelBookLast=showBookView.findViewById(R.id.label_book_last_item_home);
                TextView labelBookTimes=showBookView.findViewById(R.id.label_book_times_item_home);

                labelBookName.setText(itemData.bookToBeShow.name);
                labelBookLast.setText(itemData.bookToBeShow.last);
                labelBookTimes.setText(itemData.bookToBeShow.times+"");

                return showBookView;


            case ItemType.ADD_BOOK:
                return LayoutInflater.from(getContext()).inflate(R.layout.item_add_new, null);


            case ItemType.MANAGE_BOOK:
                View manageBookView = LayoutInflater.from(getContext()).inflate(R.layout.item_book_manager, null);

                CheckBox checkBox=manageBookView.findViewById(R.id.check_book);
                checkBox.setText(itemData.bookToBeShow.name);
                checkBox.setHighlightColor(HomeActivity.instance.getResources().getColor(R.color.themeColor));
                checkBox.setChecked(itemData.checked);

                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        itemData.checked=b;
                        ManagerActivity.instance.freshLabel();
                    }
                });
                this.checkBoxes.add(checkBox);
                return manageBookView;


            case ItemType.ADD_WORD:
                View addWordView= LayoutInflater.from(getContext()).inflate(R.layout.item_add_new, null);
                TextView textView=addWordView.findViewById(R.id.label_addBook);
                textView.setText("添加单词");
                return addWordView;

            case ItemType.SHARE_BOOK:
                View shareBookView= LayoutInflater.from(getContext()).inflate(R.layout.item_add_new, null);
                ImageView image=shareBookView.findViewById(R.id.image_add_item);
                image.setBackgroundDrawable(MainActivity.instance.getDrawable(R.drawable.share));
                TextView textViewShare=shareBookView.findViewById(R.id.label_addBook);
                textViewShare.setText("分享单词本");
                return shareBookView;

            case ItemType.SHOW_WORD:
                View showWordView=LayoutInflater.from(getContext()).inflate(R.layout.item_word_show, null);
                TextView labelName=showWordView.findViewById(R.id.label_word_name);
                TextView labelTranslate=showWordView.findViewById(R.id.label_word_translate);
                labelName.setText(itemData.wordToBeShow.name);
                labelTranslate.setText(itemData.wordToBeShow.translated.replace("\n"," "));
                return showWordView;



            case ItemType.MANAGE_WORD:
                View manageWordView=LayoutInflater.from(getContext()).inflate(R.layout.item_book_manager, null);
                CheckBox checkBoxWord=manageWordView.findViewById(R.id.check_book);
                checkBoxWord.setText(itemData.wordToBeShow.name);
                checkBoxWord.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        itemData.checked=b;
                        WordsManageActivity.instance.freshLabel();
                    }
                });
                this.checkBoxes.add(checkBoxWord);
                return manageWordView;



            default:
                return null;
        }
    }
}
